$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/rolelist',
        datatype: "json",
        colModel: [{ label: '角色ID', name: 'roleId', index: "roleId", width: 45, key: true,hidden:true},
			{ label: '角色名称', name: 'roleName', index: "roleName", width: 75 },
			{ label: '角色名称', name: 'roleName', index: "roleName", width: 75 },
			{ label: '备注', name: 'remark', width: 100 },
			{ label: '角色类型', name: 'roleType', width: 100,formatter:function (value) {if(value==1) return "平台管理角色";if(value==2) return "OEM管理角色";}},
			{ label: '所属APP', name: 'appName', width: 100 },
			{ label: '创建时间', name: 'createDate', index: "createTime", width: 80}],
		viewrecords: true, height: 385, rowNum: 10, rowList : [10,30,50], rownumbers: true, rownumWidth: 25,
		autowidth:true, multiselect: true, pager: "#jqGridPager",
        jsonReader : {root: "result.list", page: "result.currPage", total: "result.totalPage", records: "result.totalCount"},
        prmNames : {page:"page", rows:"limit", order: "order"},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {data: {simpleData: {enable: true, idKey: "menuId", pIdKey: "menuUpId", rootPId: -1},
	key: {url:"nourl", name:"menuName", children:"list"}}, check:{enable:true, nocheckInherit:true}};
var ztree;



var vm = new Vue({
	el:'#rrapp',
	data:{
		showList:true,
		isUpdate:false,
		title:null,
		appId:0,
		role:{},
		appList:{},
		loginUser:{}
	},
	methods:{
		add:function (e) {
			vm.showList=false;
			vm.isUpdate=false;
			vm.title="新增角色";
			vm.appId=0;
			vm.getLoginUser();
			vm.getMenuTree(null);
			vm.getAppList();
			vm.role={};
		},
		update:function (e) {

			var roleId = getSelectedRow();
			if(roleId == null){
				return;
			}
			vm.showList = false;
			vm.isUpdate = true;
			vm.title = "修改角色";
			vm.getMenuTree(roleId);
			vm.getLoginUser();
			vm.getAppList();
		},
		del: function (event) {
			var roleId = getSelectedRow();
			if(roleId == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.getJSON("/platform/manage/roledel/"+roleId,null,function (r) {
					if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				});
			});
		},

		saveOrUpdate: function (event) {
			//获取选择的菜单
			var nodes = ztree.getCheckedNodes(true);
			var menuList = new Array();
			for(var i=0; i<nodes.length; i++) {
				menuList.push(nodes[i]);
			}
			vm.role.menuList = menuList;
			vm.role.appId = $("#selectAppId").val();
			var url = vm.role.roleId == null ? "/platform/manage/roleadd" : "/platform/manage/roleupdate";
			$.ajax({
				type: "POST",
				url: url,
				data: JSON.stringify(vm.role),
				success: function(r){
					if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
		},


		getLoginUser:function () {
			$.get("/platform/admininfo", function(r){
				vm.loginUser= r.result;
			});
		},


		getMenuTree: function(roleId) {
			//加载菜单树
			$.get("/platform/manage/rolemenutree", function(r){
				ztree = $.fn.zTree.init($("#menuTree"), setting, r.result);
				//展开所有节点
				ztree.expandAll(true);
				if(roleId != null){
					vm.getRole(roleId);
				}
			});
			$.getJSON("/platform/manage/roleinfo/"+roleId,null,function (r) {
				vm.role = r.result;
			});
		},


		getRole: function(roleId){
			$.get("/platform/manage/rolemenus/"+roleId, function(r){
				var menuIds = r.result;
				if(menuIds){
					for(var i=0; i<menuIds.length; i++) {
						var node = ztree.getNodeByParam("menuId", menuIds[i].menuId);
						if (node == null)
							continue;
						ztree.checkNode(node, true, false);
					}
				}

			});
		},
		getAppList:function () {
			$.get("/platform/manage/selectapplist",function (r) {
				vm.appList = r.result;
			})
		},

		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		}
	}
});
